
package dialogs;

import core.COINCOMOSubComponent;
import core.COINCOMOSubSystem;
import database.COINCOMOComponentManager;
import database.COINCOMOSubComponentManager;
import database.COINCOMOSubSystemManager;
import database.COINCOMOSystemManager;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import main.GlobalMethods;
import main.Icons;
import panels.ComponentOverviewPanel;
import panels.CosysmoverviewPanel;

/**
 *
 * @author Raed Shomali
 */
public class SubSystemEAFDialog extends JDialog implements ActionListener
{
    DecimalFormat format1Decimal = new DecimalFormat( "0.0" );
    DecimalFormat format2Decimals = new DecimalFormat( "0.00" );
    //Arnab
    private JLabel[] productDriverLabels = new JLabel[]
    {
        new JLabel( "REQU" ) , new JLabel( "ARCU" ) , new JLabel( "LOSR" ) , new JLabel( "MCPY" ) , new JLabel( "TECR" )
    };

    private JLabel[] personnelDriverLabels = new JLabel[]
    {
        new JLabel( "DOCU" ) , new JLabel( "DIPT" ) , new JLabel( "RLID" ) , new JLabel( "SHTC" ) , new JLabel( "PTCY" )
    };

    private JLabel[] platformDriverLabels = new JLabel[]
    {
        new JLabel( "PECY" ) , new JLabel( "PCPY" ) , new JLabel( "MSCN" ), new JLabel( "TLST" )
    };

   
     private JButton[] productRatingButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" )
    };

     private JButton[] personnelRatingButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ) 
    };

    private JButton[] platformRatingButtons = new JButton[]
    {
        new JButton( "NOM" ) , new JButton( "NOM" ) , new JButton( "NOM" ), new JButton( "NOM" )
    };

  

     private JLabel[] driverLabels = new JLabel[]
    {
        new JLabel( "P1" ) , new JLabel( "P2" ) , new JLabel( "P3" )
    };

    private JLabel[][] driverSetLabels = new JLabel[][]
    {
        productDriverLabels ,
        personnelDriverLabels ,
        platformDriverLabels ,
    };

    private JButton[][] ratingSetButtons = new JButton[][]
    {
        productRatingButtons ,
        personnelRatingButtons ,
        platformRatingButtons ,
    };

    
    JPopupMenu ratingPopup = new JPopupMenu();

    JPopupMenu incrementPopup = new JPopupMenu();

    JMenuItem vloMenuItem = new JMenuItem( "VLO" );

    JMenuItem loMenuItem = new JMenuItem( "LO" );

    JMenuItem nomMenuItem = new JMenuItem( "NOM" );

    JMenuItem hiMenuItem = new JMenuItem( "HI" );

    JMenuItem vhiMenuItem = new JMenuItem( "VHI" );

    JMenuItem xhiMenuItem = new JMenuItem( "XHI" );

  /*  JMenuItem zeroMenutItem = new JMenuItem( "0%" );

    JMenuItem twentyFiveMenutItem = new JMenuItem( "25%" );

    JMenuItem fiftyMenutItem = new JMenuItem( "50%" );

    JMenuItem seventyFiveMenutItem = new JMenuItem( "75%" ); */

    JButton applyButton = new JButton( "Apply" );

    JButton resetButton = new JButton( "Reset" );

    JButton closeButton = new JButton( "Close" );
    // The Button From Which the Popup Generated ...
    JButton ownerButton = null;

    COINCOMOSubSystem subSystem = null;

    // For Updating the Table
    int rowNumber = 0 ;

    /**
     *
     * @param frame to be used to set the Owner of the Dialog
     * @param subComponent object to read from and save to
     * @param rowNumber to update the corresponding CLEF's SubComponent
     */
    public SubSystemEAFDialog( JFrame frame , COINCOMOSubSystem subSystem , int rowNumber )
    {
        super( frame );

        this.setModal( true );

        this.rowNumber = rowNumber ;
        this.subSystem = subSystem;

        this.setTitle( "Cost Parameters - " + subSystem.getName() );

        GlobalMethods.updateStatusBar( "Done." );

        // Popup
        ratingPopup.add( vloMenuItem );
        ratingPopup.add( loMenuItem );
        ratingPopup.add( nomMenuItem );
        ratingPopup.add( hiMenuItem );
        ratingPopup.add( vhiMenuItem );
        ratingPopup.add( xhiMenuItem );

     /*   incrementPopup.add( zeroMenutItem );
        incrementPopup.add( twentyFiveMenutItem );
        incrementPopup.add( fiftyMenutItem );
        incrementPopup.add( seventyFiveMenutItem );*/

        // Menu Items
        vloMenuItem.addActionListener( this );
        loMenuItem.addActionListener( this );
        nomMenuItem.addActionListener( this );
        hiMenuItem.addActionListener( this );
        vhiMenuItem.addActionListener( this );
        xhiMenuItem.addActionListener( this );

        

        // Buttons
        applyButton.addActionListener( this );
        resetButton.addActionListener( this );
        closeButton.addActionListener( this );

        applyButton.setFocusable( false );
        resetButton.setFocusable( false );
        closeButton.setFocusable( false );

        applyButton.setIcon( Icons.SAVE_ICON );
        resetButton.setIcon( Icons.RESET_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        this.setLayout( null );

        int ratings[] = subSystem.getRatings() ;
        int counter = 0 ;

        // GUI
        for ( int i = 0 ; i < driverSetLabels.length ; i++ )
        {
            // Legend
            TitledBorder legendTitleBorder = BorderFactory.createTitledBorder( driverLabels[i].getText() );
            legendTitleBorder.setTitleColor( Color.BLUE );

            this.add( driverLabels[i] );

            driverLabels[i].setText( "" );
            driverLabels[i].setBorder( legendTitleBorder );
            driverLabels[i].setFont( new Font( "courier" , 1 , 12 ) );

           
            driverLabels[i].setBounds( 10 , 10 + i * 100 , 530 , 100 );
            

            JLabel label1 = new JLabel( "Rating" );
           // JLabel label2 = new JLabel( "% Incr" );

            label1.setFont( new Font( "arial" , 1 , 11 ) );
          //  label2.setFont( new Font( "arial" , 1 , 11 ) );

            this.add( label1 );
          //  this.add( label2 );

           
                label1.setBounds( 40 , 10 + i * 100 + 35 , 65 , 25 );
           //     label2.setBounds( 40 , 10 + i * 100 + 65 , 65 , 25 );
            

            for ( int j = 0 ; j < driverSetLabels[i].length ; j++ )
            {
                this.add( driverSetLabels[i][j] );
                this.add( ratingSetButtons[i][j] );
                

                ratingSetButtons[i][j].setName( driverSetLabels[i][j].getText() );
                

                ratingSetButtons[i][j].setText( GlobalMethods.convertToRating( ratings[counter] ) ) ;
                //incrementSetButtons[i][j].setText( GlobalMethods.convertToPercent( percent[counter++] ) ) ;

                ratingSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );
                //incrementSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );

                ratingSetButtons[i][j].setFocusable( false );
                //incrementSetButtons[i][j].setFocusable( false );

                ratingSetButtons[i][j].addActionListener( this );
                //incrementSetButtons[i][j].addActionListener( this );

                driverSetLabels[i][j].setHorizontalAlignment( JLabel.CENTER );
                driverSetLabels[i][j].setFont( new Font( "arial" , 1 , 11 ) );

               
                    driverSetLabels[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 10 , 65 , 25 );
                    ratingSetButtons[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 35 , 65 , 25 );
                    //incrementSetButtons[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 65 , 65 , 25 );
                
            }
        }

        // Panel
        JPanel southPanel = new JPanel();
        southPanel.add( applyButton );
        southPanel.add( resetButton );
        southPanel.add( closeButton );

        this.add( southPanel );
        southPanel.setBounds( 0 , 420 , 570 , 40 );

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( false );
        this.setSize( 570 , 500 );
        this.setVisible( true );
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == applyButton )
        {
            // Disable Them ...
            applyButton.setEnabled( false );
            resetButton.setEnabled( false );
            closeButton.setEnabled( false );

            GlobalMethods.updateStatusBar( "Saving ..." );

            CosysmoverviewPanel.estimationTextPane.setText( "Loading ..." ) ;

            SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                    String ratings[]= new String[14];
                    //int percent[] = new int[ 18 ];

                    int counter = 0 ;

                    for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
                    {
                        for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
                        {
                            ratings[counter++] = ((JButton) ratingSetButtons[i][j]).getText();
                            //percent[counter++] = GlobalMethods.convertFromPercent( ((JButton) incrementSetButtons[i][j]).getText() );
                        }
                    }
                    
                    subSystem.setCosysmoRatings( ratings );
                    
                    //subSystem.setPercent( percent );

                    // Update Sub Component with Values ...
                    //COINCOMOSubSystemManager.updateSubSystem( subSystem );
                    COINCOMOSubSystemManager.updateCosysmoCost(subSystem);
                    COINCOMOSystemManager.updateSystem( CosysmoverviewPanel.system ) ;

                    // Update Table ...
                    CosysmoverviewPanel.clefTable.setValueAt( format2Decimals.format( GlobalMethods.roundOff( subSystem.getEaf() , 2 ) ) , rowNumber , 4 ) ;
                    

                    // Update Summary Report
                    CosysmoverviewPanel.updateEstimationTextPane( false );

                    GlobalMethods.updateStatusBar( "Saved." );

                    // Enable Them ...
                    applyButton.setEnabled( true );
                    resetButton.setEnabled( true );
                    closeButton.setEnabled( true );
                }

            } );
        }
        else if ( e.getSource() == resetButton )
        {
            for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
            {
                for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
                {
                    JButton button = ratingSetButtons[i][j];

                    button.setText( "NOM" );
                }
            }

           /* for ( int i = 0 ; i < incrementSetButtons.length ; i++ )
            {
                for ( int j = 0 ; j < incrementSetButtons[i].length ; j++ )
                {
                    JButton button = incrementSetButtons[i][j];

                    button.setText( "0%" );
                }
            } */
        }
        else if ( e.getSource() == closeButton )
        {
            // Free Resources ... Close Window
            this.dispose();
        }
        else if ( e.getSource() == vloMenuItem || e.getSource() == loMenuItem || e.getSource() == nomMenuItem ||
                e.getSource() == hiMenuItem || e.getSource() == vhiMenuItem || e.getSource() == xhiMenuItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        }
        /*else if ( e.getSource() == zeroMenutItem || e.getSource() == twentyFiveMenutItem ||
                e.getSource() == fiftyMenutItem || e.getSource() == seventyFiveMenutItem )
        {
            ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
        } */
        else
        {
            ownerButton = ((JButton) e.getSource());

            // Default
            vloMenuItem.setEnabled( true );
            loMenuItem.setEnabled( true );
            nomMenuItem.setEnabled( true );
            hiMenuItem.setEnabled( true );
            vhiMenuItem.setEnabled( true );
            xhiMenuItem.setEnabled( true );

            if( ownerButton.getName().equals( "RELY" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "ARCU" ) )
            {
                //vloMenuItem.setEnabled( false );
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "LOSR" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "MCPY" ) )
            {
                //xhiMenuItem.setEnabled( false );
                vloMenuItem.setEnabled( false );
                loMenuItem.setEnabled(false);
            }
            else if( ownerButton.getName().equals( "TECR" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "DOCU" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "DIPT" ) )
            {
                vloMenuItem.setEnabled( false );
                loMenuItem.setEnabled(false);
            }
            else if( ownerButton.getName().equals( "RLID" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "SHTC" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "PTCY" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "PECY" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "PCPY" ) )
            {
                //xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "MSCN" ) )
            {
                //xhiMenuItem.setEnabled( false );
            }
            else if( ownerButton.getName().equals( "TLST" ) )
            {
                xhiMenuItem.setEnabled( false );
            }
           
            

            // Determine Which Popup to show ...
            if ( ownerButton.getText().endsWith( "%" ) )
            {
                incrementPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
            else
            {
                ratingPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
            }
        }
    }

}
